package cn.tedu.charging.web;

import cn.tedu.charging.common.ex.ServiceException;
import lombok.Data;

/**
 * 服务器端的统一响应类型
 *
 * @author wyy
 * @version 1.0
 */
@Data
public class JsonResult {
//    private Integer code;
//    private String message;
//    private Object data;
//
//    public JsonResult(Integer code, String message, Object data) {
//        this.code = code;
//        this.message = message;
//        this.data = data;
//    }
//
//    public JsonResult() {
//    }

    /**
     * 响应的业务状态码值
     */
    private Integer state;

    /**
     * 操作失败时的描述文本
     */
    private String message;

    /**
     * 操作成功时响应的数据
     */
    private Object data;

    public static JsonResult ok() {
        return ok(null,null);
    }

    public static JsonResult ok(String message, Object data) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(ServiceCode.OK.getValue());
        jsonResult.setMessage(message);
        jsonResult.setData(data);
        return jsonResult;
    }

    public static JsonResult fail(ServiceException e) {
        return fail(e.getServiceCode(), e.getMessage());
    }

    public static JsonResult fail(ServiceCode serviceCode, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(serviceCode.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }
}
